package com.feng.zshop.backend.controller;

import com.feng.zshop.backend.param.SysuserParam;
import com.feng.zshop.backend.service.RoleService;
import com.feng.zshop.backend.service.SysuserService;
import com.feng.zshop.backend.vo.LoginVO;
import com.feng.zshop.backend.vo.SysuserVO;
import com.feng.zshop.common.constant.PaginationConstant;
import com.feng.zshop.common.exception.ServiceException;
import com.feng.zshop.common.utils.ResponseResult;
import com.feng.zshop.model.Role;
import com.feng.zshop.model.Sysuser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/backend/sysuser")
public class SysuserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(SysuserController.class);

    @Autowired
    private SysuserService sysuserService;

    @Autowired
    private RoleService roleService;

    @PostMapping("/login")
    public String login(@Valid LoginVO loginVO, HttpSession session, Model model) {
        String verificationCode = (String) session.getAttribute("verificationCode");

        // 验证码为空时直接返回登陆页面
        if (verificationCode == null || verificationCode.equals("")) {
            model.addAttribute("errorMsg", "验证码不能为空");
            return "login";
        }

        if (!verificationCode.equalsIgnoreCase(loginVO.getVerificationCode())) {
            model.addAttribute("errorMsg", "验证码错误");
            return "login";
        }

        Sysuser sysuser = sysuserService.findByLoginNameAndPassword(loginVO.getLoginName(), loginVO.getPassword());
        if (sysuser == null) {
            model.addAttribute("errorMsg", "用户名或密码错误");
            return "login";
        }
        return "main";
    }

    @GetMapping("/findAll")
    public String findAllUser(@RequestParam(value = "pageNum", required = false) Integer pageNum, Model model) {
        if (pageNum == null) {
            pageNum = PaginationConstant.PAGE_NUMBER;
        }
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<Sysuser> sysusers = sysuserService.findAllSysuser();
        PageInfo<Sysuser> pageInfo = new PageInfo<>(sysusers);
        model.addAttribute("pageInfo",pageInfo);
        return "sysuserManager";
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseResult add(@Valid SysuserVO sysuserVO) {
        try {
            sysuserService.add(sysuserVO);
        } catch (ServiceException e) {
            return ResponseResult.failNoData(e.getMessage());
        }
        return ResponseResult.successNoData("用户保存成功");
    }

    @PostMapping("/findByParams")
    public String sysuserParam(@RequestParam(value = "pageNum") Integer pageNum, SysuserParam sysuserParam, Model model){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<Sysuser> sysusers = sysuserService.selectByCondition(sysuserParam);
        PageInfo<Sysuser> pageInfo = new PageInfo<>(sysusers);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("sysuserParam",sysuserParam);
        return "sysuserManager";
    }

    @PostMapping("/modify")
    @ResponseBody
    public ResponseResult modify(@Valid SysuserVO sysuserVO) {
        try {
            sysuserService.modify(sysuserVO);
        } catch (ServiceException e) {
            return ResponseResult.failNoData(e.getMessage());
        }
        return ResponseResult.successNoData("用户更新成功");
    }

    @PostMapping("/modifyStatus")
    @ResponseBody
    public ResponseResult modifyStatus(@RequestParam(value = "id") Integer id) {
        sysuserService.modifyStatusById(id);
        return ResponseResult.successNotMessageAndData();
    }

    @ModelAttribute("roles")
    public List<Role> getRoles() {
        List<Role> roles = roleService.findAllRole();
        return roles;
    }

}
